<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  add_crumb(t('#crumbs.people', "People"), context_url(@context, :context_users_url))
  add_crumb(context_user_name(@context, @user), context_url(@context, :context_user_url, @user))
  @active_tab="people"
%>
<% content_for :page_title do %><%= join_title context_user_name(@context, @user), @context.name %><% end %>

<% content_for :right_side do %>
  <h2><%= t 'links.more_about_this_user', "More About This User" %></h2>
  <div class="page-action-list rs-margin-bottom" id="right_nav">
    <% if can_do(@membership, @current_user, :read_grades) && @membership.is_a?(StudentEnrollment) %>
      <a href="<%= context_url(@context, :controller => :gradebooks, :action => :grade_summary, :id => @user.id) %>">
        {{> icons/check-plus}}
        <%= t 'links.user_grades', "Grades" %>
      </a>
    <% end %>
    <% if can_do(@context, @current_user, :send_messages) %>
      <a href="<%= message_user_path(@user) %>">
        {{> icons/email}}
        <%= t 'links.message_user', "Send Message" %>
      </a>
    <% end %>
    <% if @domain_root_account.enable_user_notes and can_do(@user, @current_user, :read_user_notes) %>
      <a href="<%= user_user_notes_path(@user) %>"> <%= t 'links.faculty_journal', "Faculty Journal" %></a>
    <% end %>
    <% if @context.is_a?(Course)%>
      <% if @context.user_is_instructor?(@user) && can_do(@user, @current_user, :read_reports) %>
        <a href="<%= user_course_teacher_activity_url(@user, @context) %>">
          {{> icons/chat}}
          <%= t 'links.teacher_activity', 'Student Interactions Report' %>
        </a>
      <% elsif @context.user_has_been_student?(@user) && @context.user_is_instructor?(@current_user) && can_do(@context, @current_user, :read_reports) %>
        <%= link_to(user_student_teacher_activity_url(@current_user, @user)) do %>
          {{> icons/chat}}
          {{#t}}Interactions Report{{/t}}
        <% end %>
      <% end %>
    <% end %>
    <% if can_do(@context, @current_user, :read_reports) %>
      <a href="<%= context_url(@context, :context_user_usage_url, @user) %>">
        {{> icons/clock}}
        <%= t 'links.access_report', "Access Report" %>
      </a>
    <% end %>
    <% if @context.is_a?(Course) && @context.has_outcomes? && can_do(@context, @current_user, :manage) %>
      <a href="<%= context_url(@context, :context_user_outcomes_results_url, @user.id) %>">
        {{> icons/outcomes}}
        <%= t "Outcomes Report" %>
      </a>
    <% end %>
    <% if @user.grants_right?(@current_user, :view_statistics) %>
      <a href="<%= user_path(@user.id) %>">
        {{> icons/user}}
        {{#t}}User Account Details{{/t}}
      </a>
    <% end %>
    <% roster_user_custom_links(@user).each do |link| %>
      <a href="<%= link[:url] %>">
        <i class="<%= link[:icon_class] %>" aria-hidden="true"></i>
        <%= link[:text] %>
      </a>
    <% end %>
  </div>
  <a href="<%= user_profile_path(@current_user) %>" class="profile_url" style="display: none;">&nbsp;</a>
  <h2><%= t 'headings.user_services', "Registered Web Services" %></h2>
  <div>
    <% if can_do(@membership, @current_user, :read_services) && !@membership.user.user_services.empty? %>
      <p>
      <% @membership.user.user_services.each do |service| %>
        <% service_name = service.service.titleize rescue 'Unknown' %>
        <a href="<%= service.service_user_link %>" title="<%= service_name %>" class="no-hover"><%= image_tag "#{service.service}_icon.png", :alt => service_name %></a>
      <% end %>
      </p>
    <% else %>
      <% if !@membership.user.user_services.empty?  %>
        <p>
          <%= t 'services_not_visible', "%{user}'s registered services are not visible to you at this time.", :user => @user.short_name %>
        </p>
      <% else %>
        <p>
          <%= t 'no_services', "%{user} hasn't linked any external services to their account yet.", :user => @user.short_name %>
        </p>
      <% end %>
    <% end %>
    <% if @current_user && @current_user == @user && @current_user.user_services.empty? %>
      <p>
        <%= t 'help.services', %{You haven't linked your user profile to any external services. You can link your Canvas account to services like Twitter. This will make it easier for fellow course and group members to link up with you outside of class.} %>
      </p>
      <p>
        <a href="<%= user_profile_path(@current_user) %>#registered_web_services" class="btn btn-primary">{{> icons/link}} {{#t}}Link web services to my account{{/t}}</a>
      </p>
    <% elsif @current_user && @current_user == @user && !@current_user.show_user_services %>
      <p>
        <%= t 'help.list_services', %{You haven't enabled your list of registered services. Enabling this list will let fellow course and group members more easily link up with you outside of class.} %>
      </p>
      <p>
        <input type="checkbox" id="show_user_services" class="show_user_services_checkbox"/><label for="show_user_services">{{#t}}Let fellow course/group members see which services I've linked to my profile{{/t}}</label>
      </p>
    <% end %>
  </div>
<% end %>

<% js_bundle :user_name %>
<% css_bundle :roster_user %>

<h1><%= @user.name %></h1>
<% can_manage_students = can_do(@context, @current_user, :manage_students) %>
<% can_manage_admins = can_do(@context, @current_user, :manage_admin_users) %>

<% if !@context.is_a?(Group) && (can_manage_students || can_manage_admins) %>
{{> views/users/name}}
<a href="#" class="more_user_information_link">{{#t}}more user details...{{/t}}</a>
  <div class="more_user_information" style="display: none;">
    <% if @context.is_a?(Course) %>
      <fieldset>
        <legend><h3>{{#t}}Membership(s){{/t}}</h3></legend>
        <table class="ic-Table">
          <% @enrollments.sort_by(&:id).each do |enrollment| %>
            <tr class="enrollment">
              <td>
                <b><a href="<%= context_url(@context, :context_section_url, enrollment.course_section_id) rescue "#" %>"><%= enrollment.try_rescue(:course_section).try_rescue(:display_name) || enrollment.short_name %></a></b>
                <div><%= t 'enrolled_as', "Enrolled as a %{enrollment_type}", :enrollment_type => enrollment.readable_type %></div>
                <div>created <%= datetime_string(enrollment.created_at) %></div>
                <% if enrollment.is_a?(ObserverEnrollment) %>
                  <div style="font-size: 0.8em; padding-left: 20px; <%= hidden unless enrollment.associated_user %>" class="associated_user" >
                    <%= t 'enrollment_linked_to', "linked to *%{linked_user}*",
                        :linked_user => context_user_name(@context, enrollment.associated_user),
                        :wrapper => '<span class="associated_user_name">\1</span>' %>
                  </div>
                <% end %>
                <div style="font-size: 0.8em; padding-left: 20px; <%= hidden unless enrollment.completed? || enrollment.inactive? %>" class="completed_at_holder">
                  <% if enrollment.workflow_state == 'inactive' %>
                    <%= t 'inactive' %>
                  <% elsif enrollment.completed_at %>
                    <%= t 'completed_enrollment', "completed *%{completed_at}*", :completed_at => datetime_string(enrollment.completed_at), :wrapper => '<span class="completed_at">\1</span>' %>
                  <% else %>
                    <%= t 'completed' %>
                  <% end %>
                </div>
              </td>
              <% if enrollment.admin? ? (can_manage_admins && enrollment.user_id != @current_user.id) : can_manage_students %>
                <td>
                  <% unless @context.completed? %>
                    <div class="conclude_enrollment_link_holder" style="margin-bottom: 5px; <%= hidden if enrollment.completed? %>">
                      <a class="conclude_enrollment_link" href="<%= context_url(@context, :controller => :courses, :action => :conclude_user, :id => enrollment.id) %>">{{#t}}Conclude this Enrollment{{/t}}</a>
                    </div>
                    <div class="unconclude_enrollment_link_holder" style="margin-bottom: 5px; <%= hidden unless enrollment.explicitly_completed? || enrollment.inactive? %>">
                      <a class="unconclude_enrollment_link" href="<%= context_url(@context, :controller => :courses, :action => :unconclude_user, :id => enrollment.id) %>">{{#t}}Restore this Enrollment{{/t}}</a>
                    </div>
                  <% end %>
                  <div><% if enrollment.defined_by_sis? %>
                    <a href="#" title="{{#t}}Can't Delete{{/t}}</a>
                  <% else %>
                    <a class="delete_enrollment_link" href="<%= context_url(@context, :context_unenroll_url, enrollment.id) %>">{{#t}}Delete this Enrollment{{/t}}</a>
                  <% end %>
                  </div>
                </td>
              <% end %>
            </tr>
          <% end %>
          <% if @context.is_a?(Course) && can_do(@context, @current_user, :manage_admin_users) %>
            <tr>
              <td>
                <%= before_label('user_privileges', %{Privileges}) %>
              </td>
              <td>
                <div class="elevate_enrollment_link_holder" style="<%= hidden if @enrollments.any?{|e| !e.limit_privileges_to_course_section } %>">
                  {{#t}}this user can only view students in their assigned course section(s){{/t}}
                  <div style="margin-left: 10px; font-size: 0.8em;">
                    <a href="<%= context_url(@context, :context_limit_user_grading_url, @user.id) %>" class="elevate_enrollment_link">{{#t}}let this user see all course users{{/t}}</a>
                  </div>
                </div>
                <div class="restrict_enrollment_link_holder" style="<%= hidden unless @enrollments.any?{|e| !e.limit_privileges_to_course_section } %>">
                  {{#t}}this user can view students in any course section{{/t}}
                  <div style="margin-left: 10px; font-size: 0.8em;">
                    <a href="<%= context_url(@context, :context_limit_user_grading_url, @user.id) %>" class="restrict_enrollment_link">{{#t}}limit this user to only see fellow section users{{/t}}</a>
                  </div>
                </div>
              </td>
            </tr>
          <% end %>
        </table>
      </fieldset>
      {{> views/courses/link_enrollment}}
    <% end %>
    {{> views/users/logins}}
  </div>
<div style="margin-bottom: 10px;">&nbsp;</div>
<% elsif service_enabled?(:avatars) %>
  <div style="margin-bottom: 20px;">
    <%= avatar(@user) %>
    <% if can_do(@user, @current_user, :remove_avatar) %>
      <a href="#" class="remove_avatar_picture_link">{{#t}}Remove avatar picture{{/t}}</a>
    <% else %>
      <a href="<%= report_avatar_image_url(@user.id) %>" class="report_avatar_picture_link">{{#t}}Report inappropriate picture{{/t}}</a>
    <% end %>
  </div>
<% end %>

<% if @messages %>
<h2>{{#t}}Recent Messages{{/t}}</h2>
<%= t('no_messages', "No Messages") if @messages.empty? %>
<% @messages[0,10].each do |message| %>
  <% if message.is_a?(DiscussionEntry) %>
    {{> views/discussion_topics/entry}}
  <% end %>
<% end %>
<% end %>

<% js_bundle 'legacy/context_roster_user' %>
